<?
function drizzo_get_the_post($query_id = 0) {
	drizzo_db_connect();

	global $drizzo_posts, $drizzo_post_id, $drizzo_comments;
	
	$the_post = mysql_query("SELECT * FROM `$drizzo_posts` WHERE `ID`='$query_id' LIMIT 1");
	if(!$the_post) {
		echo "<p>Post could not be found.</p>";
		return false;
	} else {
		while($row = mysql_fetch_array($the_post)) {
			$author_query = mysql_query("SELECT `nice_name`,`username` FROM `$drizzo_users` WHERE `ID`='$row[author]'");
			if($author_query) {
				while($author_row = mysql_fetch_array($author_query)) {
					$author = $author_row['nice_name'];
					if($author == "") {
						$author = $author_row['username'];
					}
				}				
			}
			$comments_query = mysql_query("SELECT * FROM `$drizzo_comments` WHERE `post_id`='$query_id' AND `status` LIKE '%approved%'");
			
			if($comments_query) {
				$comments = mysql_num_rows($comments_query);				
			} else {
				$comments = 0;
			}

			$the_post = array(
				"ID" => $query_id,
				"author" => $row['author'],
				"author_nicename" => $author,
				"type" => $row['type'],
				"status" => $row['status'],
				"page_parent" => $row['page_parent'],
				"page_order" => $row['page_order'],
				"title" => stripslashes($row['title']),
				"content" => stripslashes($row['content']),
				"tags" => $row['tags'],
				"comment_status" => $row['comment_status'],
				"comment_count" => $comments,
				"posted_date" => (date("M d, Y", $row['posted_date']) . " at " . date("g:i A", $row['posted_date'])),
				"posted_date_raw" => $row['posted_date'],
				"edited_date" => (date("M d, Y", $row['edited_date']) . " at " . date("g:i A", $row['edited_date'])),
				"edited_date_raw" => $row['edited_date'],
				"edited_by" => $row['edited_by'],
				"menu_name" => $row['menu_name'],
				"permalink_name" => $row['permalink_name'],
				"lat" => $row['lat'],
				"lon" => $row['lon'],
				"old_id" => $row['old_id']
			);

			return $the_post;

		}
	}
}

function drizzo_get_the_posts($limit = 20, $offset = 0, $status = "publish,private", $types = "blog,aside") {
	global $drizzo_posts, $drizzo_comments, $drizzo_users;
	
	$statuses = explode(",", $status);
	for($i =0; $i < count($statuses); $i++) {
		if($i < (count($statuses) -1)) {
			$status_string .= "'" . $statuses[$i] . "', ";
		} else {
			$status_string .= "'" . $statuses[$i] . "'";
		}
	}
	
	$status = $status_string;

	$type_list = explode(",", $types);
	for($i =0; $i < count($type_list); $i++) {
		if($i < (count($type_list) -1)) {
			$type_string .= "'" . $type_list[$i] . "', ";
		} else {
			$type_string .= "'" . $type_list[$i] . "'";
		}
	}
	
	$types = $type_string;
	
	drizzo_db_connect();

	if($limit == 0) {
		$the_posts_query = mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status` IN ($status) AND `type` IN ($types) ORDER BY `ID` DESC");
	} else {
		$the_posts_query = mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status` IN ($status) AND `type` IN ($types) ORDER BY `ID` DESC LIMIT $offset,$limit");
	}

	if(!$the_posts_query) {
		echo "<p>Posts could not be retrieved.</p>";
	} else {
		while($row = mysql_fetch_array($the_posts_query)) {
			$author_query = mysql_query("SELECT `nice_name`,`username` FROM `$drizzo_users` WHERE `ID`='$row[author]'");
			if($author_query) {
				while($author_row = mysql_fetch_array($author_query)) {
					$author = $author_row['nice_name'];
					if($author == "") {
						$author = $author_row['username'];
					}
				}				
			}

			$comments_query = mysql_query("SELECT * FROM `$drizzo_comments` WHERE `post_id`='$row[ID]'");
			if($comments_query) {
				$comments = mysql_num_rows($comments_query);				
			} else {
				$comments = 0;
			}

			$the_posts[] = array(
				"ID" => $row['ID'],
				"author" => $row['author'],
				"author_nicename" => $author,
				"type" => $row['type'],
				"status" => $row['status'],
				"page_parent" => $row['page_parent'],
				"page_order" => $row['page_order'],
				"title" => stripslashes($row['title']),
				"content" => stripslashes($row['content']),
				"tags" => $row['tags'],
				"comment_status" => $row['comment_status'],
				"comment_count" => $comments,
				"posted_date" => (date("M d, Y", $row['posted_date']) . " at " . date("g:i A", $row['posted_date'])),
				"posted_date_raw" => $row['posted_date'],
				"edited_date" => (date("M d, Y", $row['edited_date']) . " at " . date("g:i A", $row['edited_date'])),
				"edited_date_raw" => $row['edited_date'],
				"edited_by" => $row['edited_by'],
				"menu_name" => $row['menu_name'],
				"permalink_name" => $row['permalink_name'],
				"lat" => $row['lat'],
				"lon" => $row['lon']
				);
				
		}
	}
	return $the_posts;
}

function drizzo_get_the_asides($limit = 1) {
	$the_posts = drizzo_get_the_posts($limit, 0, "publish", "aside");
	return $the_posts;
}

function drizzo_asides($limit = 1) {
	$the_posts = drizzo_get_the_asides($limit);
	$asides_string .= "<ul id=\"asides\">\n";
	if(count($the_posts) > 0) {
		foreach($the_posts as $the_post) {
			$asides_string .= "\t<li class=\"aside\"><strong>" . $the_post['title'] . "</strong><br />" . $the_post['content'] . "</li>\n";
		}

	} else {
		$asides_string .= "<li>There are no asides.</li>\n";
	}
	$asides_string .= "</ul>\n";
	return $asides_string;
}

function drizzo_post_id() {
	global $the_post;

	$id= "";
	
	if(!$the_post) {
		return false;
	} else {
		$id = $the_post['ID'];
		return $id;
	}
}

function drizzo_post_title() {
	global $the_post;
	
	$title= "";
	
	if(!$the_post) {
		return false;
	} else {
		$title = $the_post['title'];
		return $title;
	}
}

function drizzo_post_author($var = "ID") {
	global $the_post, $drizzo_users;
	
	$author = "";
	
	if(!$the_post) {
		return false;
	} else {

		if($var == "ID") {
			$author = $the_post['author'];
		}

		if(($var == "nice_name") || ($var == "nickname") || ($var == "nicename")) {
			$author_id = $the_post['author'];
			$author_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$author_id'");
			if($author_lookup) {
				while($row = mysql_fetch_array($author_lookup)) {
					$author = $row['nice_name'];
				}
			}
		}

		if(($var == "firstname") || ($var == "first_name") || ($var == "first")) {
			$author_id = $the_post['author'];
			$author_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$author_id'");
			if($author_lookup) {
				while($row = mysql_fetch_array($author_lookup)) {
					$author = $row['first_name'];
				}
			}
		}

		if(($var == "lastname") || ($var == "last_name") || ($var == "last")) {
			$author_id = $the_post['author'];
			$author_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$author_id'");
			if($author_lookup) {
				while($row = mysql_fetch_array($author_lookup)) {
					$author = $row['last_name'];
				}
			}
		}
		
		if(($var == "fullname") || ($var == "full_name")) {
			$author_id = $the_post['author'];
			$author_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$author_id'");
			if($author_lookup) {
				while($row = mysql_fetch_array($author_lookup)) {
					$author = $row['first_name'] . " " . $row['last_name'];
				}
			}
		}

		return $author;
	}
}

function drizzo_post_status() {
	global $the_post;
	
	$status = "";
	
	if(!$the_post) {
		return false;
	} else {
		$status = $the_post['status'];
		return $status;
	}
}

function drizzo_post_parent() {
	global $the_post;
	
	$parent = "";
	
	if((!$the_post) || ($the_post['type'] != "page")) {
		return false;
	} else {
		$parent = $the_post['page_parent'];
		return $parent;
	}
}

function drizzo_post_order() {
	global $the_post;
	
	$order = "";
	
	if((!$the_post) || ($the_post['type'] != "page")) {
		return false;
	} else {
		$order = $the_post['page_order'];
		return $order;
	}
}

function drizzo_post_content($length = null, $word_count = 150) {
	global $the_post;
	
	$content = "";
	$excerpt = "";
	
	if(!$the_post) {
		return false;
	} else {
		$content = $the_post['content'];
		
		if(!$length || ($length == "excerpt" && str_word_count($content) <= $word_count)) {
			return $content;
		}
		
		if($length == "excerpt" && str_word_count($content) > $word_count) {
			$excerpt = strip_tags($content);
			$excerpt = explode(" ", $excerpt);
			for($i = 0; $i < $word_count; $i++) {
				$content_string .= $excerpt[$i] . " ";
			}
			$content_string .= "<p><a href=\"" . drizzo_post_permalink() . "\">Read on...</a></p>";
			return $content_string;
		}
	}
}

function drizzo_post_excerpt($word_count = 150) {
	$excerpt = drizzo_post_content("excerpt", $word_count);
	return $excerpt;
}

function drizzo_post_date($var = "all") {
	global $the_post;
	
	$date = "";
	
	if(!$the_post) {
		return false;
	} else {
		if($var == "all") {
			$date = $the_post['posted_date'];
			return $date;
		} elseif($var == "year") {
			$date = $the_post['posted_date_raw'];
			$date = date("Y", $date);
			return $date;
		} elseif($var == "month") {
			$date = $the_post['posted_date_raw'];
			$date = date("m", $date);
			return $date;
		} elseif($var == "date") {
			$date = $the_post['posted_date_raw'];
			$date = date("d", $date);
			return $date;
		} elseif($var == "day") {
			$date = $the_post['posted_date_raw'];
			$date = date("l", $date);
			return $date;
		} elseif($var == "hour") {
			$date = $the_post['posted_date_raw'];
			$date = date("h", $date);
			return $date;
		} elseif($var == "hour_24") {
			$date = $the_post['posted_date_raw'];
			$date = date("H", $date);
			return $date;
		} elseif(($var == "minutes") || ($var == "minute")) {
			$date = $the_post['posted_date_raw'];
			$date = date("m", $date);
			return $date;
		} elseif($var == "seconds") {
			$date = $the_post['posted_date_raw'];
			$date = date("s", $date);
			return $date;
		} else {
			$date = $the_post['posted_date_raw'];
			$date = date($var, $date);
			return $date;
		}
	}
}

function drizzo_post_edit_date() {
	global $the_post;
	
	$edit_date = "";
	
	if(!$the_post) {
		return false;
	} else {
		$edit_date = $the_post['edited_date'];
		return $edit_date;
	}
}

function drizzo_post_editor($var = "ID") {
	global $the_post, $drizzo_users;
	
	$editor = "";
	
	if(!$the_post) {
		return false;
	} else {

		if($var == "ID") {
			$editor = $the_post['edited_by'];
		}

		if(($var == "nice_name") || ($var == "nickname") || ($var == "nicename")) {
			$editor_id = $the_post['edited_by'];
			$editor_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$editor_id'");
			if($editor_lookup) {
				while($row = mysql_fetch_array($editor_lookup)) {
					$editor = $row['nice_name'];
				}
			}
		}

		if(($var == "firstname") || ($var == "first_name") || ($var == "first")) {
			$editor_id = $the_post['editor'];
			$editor_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$editor_id'");
			if($editor_lookup) {
				while($row = mysql_fetch_array($editor_lookup)) {
					$editor = $row['first_name'];
				}
			}
		}

		if(($var == "lastname") || ($var == "last_name") || ($var == "last")) {
			$editor_id = $the_post['editor'];
			$editor_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$editor_id'");
			if($editor_lookup) {
				while($row = mysql_fetch_array($editor_lookup)) {
					$editor = $row['last_name'];
				}
			}
		}
		
		if(($var == "fullname") || ($var == "full_name")) {
			$editor_id = $the_post['editor'];
			$editor_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$editor_id'");
			if($editor_lookup) {
				while($row = mysql_fetch_array($editor_lookup)) {
					$editor = $row['first_name'] . " " . $row['last_name'];
				}
			}
		}

		return $editor;
	}
}

function drizzo_post_tags($id = 0, $style = "comma", $links = "true") {
	global $drizzo_posts, $the_post;
	
	drizzo_db_connect();
	
	if($id == 0) {
		$id = $the_post['ID'];
	}
	
	if($id == 0) {
		return false;
	} else {
		if($style == "comma") {
			$tags_query = mysql_query("SELECT `tags` FROM `$drizzo_posts` WHERE `ID`='$id'");
			if($tags_query) {
				while($row = mysql_fetch_array($tags_query)) {
					$tags = $row['tags'];
				}

				$tag = explode(", ", $tags);
				$tag_lower = array_map('strtolower', $tag);
				array_multisort($tag_lower, SORT_ASC, SORT_STRING, $tag);
				$tag_count = count($tag);
					for($no = 0; $no < $tag_count; $no++) {
						if($links == "true") {
							$tag_nice[$no] = preg_replace("/-/", " ", $tag[$no]);
							if(($tag_count - $no) > 1) {
								$tags_string .= "<a href=\"/tags/" . $tag[$no] . "\">" . $tag_nice[$no] . "</a>, ";
							} else {
								$tags_string .= "<a href=\"/tags/" . $tag[$no] . "\">" . $tag_nice[$no] . "</a>";
							}
						} else {
							$tag_nice[$no] = preg_replace("/-/", " ", $tag[$no]);
							if(($tag_count - $no) > 1) {
								$tags_string .= $tag[$no] . ", ";
							} else {
								$tags_string .= $tag[$no];
							}
						}
					}

				return $tags_string;

			} else {
				return false;
			}
		}
		
		if($style == ("list" || "ul" || "ol")) {
			if($style == ("list" || "ul")) { $list_type = "ul"; }
			if($style ==("ol")) { $list_type = "ol"; }
			
			$tags_query = mysql_query("SELECT `tags` FROM `$drizzo_posts` WHERE `ID`='$id'");
			if($tags_query) {
				while($row = mysql_fetch_array($tags_query)) {
					$tags = $row['tags'];
				}
				$tag = explode(", ", $tags);
				$tag_lower = array_map('strtolower', $tag);
				array_multisort($tag_lower, SORT_ASC, SORT_STRING, $tag);
				$tag_count = count($tag);
				$tags_string .= "<" . $list_type . " id=\"tags-list\">\n";
					for($no = 0; $no < $tag_count; $no++) {
						if($links == "true") {
							$tag_nice[$no] = preg_replace("/-/", " ", $tag[$no]);
							$tags_string .= "\t<li class=\"tag\"><a href=\"/tags/" . $tag[$no] . "\">" . $tag_nice[$no] . "</a></li>\n";
						} else {
							$tag_nice[$no] = preg_replace("/-/", " ", $tag[$no]);
							$tags_string .= "\t<li class=\"tag\">" . $tag[$no] . "</li>\n";
						}
					}
				$tags_string .= "</" . $list_type . ">\n";

				return $tags_string;
			} else {
				return false;
			}
		}
	}
}

function drizzo_all_the_tags($style = "list") {
	global $drizzo_posts;
	
	drizzo_db_connect;
	
	$tags_query = mysql_query("SELECT `tags` FROM `$drizzo_posts` ORDER BY `ID`");
	if($tags_query) {
		while($row = mysql_fetch_array($tags_query)) {
			$tags = $row['tags'];
			$tag = explode(", ", $tags);
			$tag_count = count($tag);
			for($i = 0; $i < $tag_count; $i++) {
				$tags_list[] .= $tag[$i];
			}
		}

		$tags_list_count = count($tags_list);

		sort($tags_list);

		for($i = 0; $i < $tags_list_count; $i++) {
			$prev = $i - 1;
			if($tags_list[$i] != $tags_list[$prev]) {
				$tags_string[] .= $tags_list[$i];
			}
		}

		$tags_string_count = count($tags_string);
		
		sort($tags_string);

		if($style == "list") {
			for($i = 0; $i < $tags_string_count; $i++) {
				if($i < ($tags_string_count - 1)) {
					$clean_tag = preg_replace("/-/", " ", $tags_string[$i]);
					$all_tags .= $clean_tag . ", ";
				} else {
					$clean_tag = preg_replace("/-/", " ", $tags_string[$i]);
					$all_tags .= $clean_tag;
				}
			}
		}

		if($style == "array") {
			for($i = 0; $i < $tags_string_count; $i++) {
				$all_tags[]['tag'] = $tags_string[$i];
			}
		}

		return $all_tags;

	} else {
		return false;
	}
}

function drizzo_the_tag_list($style = "plain", $limit = null) {
	global $drizzo_posts;
	
	drizzo_db_connect();
	
	$tags = drizzo_all_the_tags("array");
	$tag_no = 0;
	foreach($tags as $tag) {
		$tag_count = 0;
		$tag_lookup = mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `tags` LIKE '%$tag[tag]%'");
		if($tag_lookup) {
			while($row = mysql_fetch_array($tag_lookup)) {
				$tag_count++;
			}
			$tag_array[$tag_no]['count'] = $tag_count;
		} else {
			$tag_array[$tag_no]['count'] = 0;
		}
		$tag_array[$tag_no]['tag'] = $tag['tag'];
		$tag_array[$tag_no]['clean'] = preg_replace("/-/", " ", $tag['tag']);
		$tag_no++;
	}
	
	if($limit) {
		rsort($tag_array);
		$tag_array = array_chunk($tag_array, $limit);
		$tag_array = $tag_array[0];
		if($style == "cloud") {
			$tag_no = 0;
			foreach($tag_array as $tag) {
				$tags_array[$tag_no]['tag'] = $tag['tag'];
				$tags_array[$tag_no]['count'] = $tag['count'];
				$tags_array[$tag_no]['clean'] = preg_replace("/-/", " ", $tag['tag']);
				$tag_no++;
			}
			
			$tag_array = $tags_array;
			sort($tag_array);
		}
	}
	
	if($style == "plain") {
		foreach($tag_array as $tag) {
			$tag_list .= "<span class=\"tag plain\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></span>";
		}
	}
	
	if($style == "comma") {
		$loop_count = 0;
		foreach($tag_array as $tag) {
			if($loop_count < count($tags_array)) {
				$tag_list .= "<span class=\"tag comma\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></span>, ";
			} else {
				$tag_list .= "<span class=\"tag comma\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></span>";
			}
			$loop_count++;
		}
	}

	if(($style == "list") || ($style == "ul")) {
		$tag_list .= "<ul id=\"tag_ul\">\n";
		foreach($tag_array as $tag) {
			$tag_list .= "\t<li class=\"tag\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></li>\n";
		}
		$tag_list .= "</ul>\n";
	}

	if($style == "ol") {
		$tag_list .= "<ol id=\"tag_ul\">\n";
		foreach($tag_array as $tag) {
			$tag_list .= "\t<li class=\"tag\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></li>\n";
		}
		$tag_list .= "</ol>\n";
	}
	
	if($style == "cloud") { // Based on tutorial @ http://www.prism-perfect.net/archive/php-tag-cloud-tutorial/
		$tag_list .= "<div id=\"tag_cloud\">\n";
		$max_size = 180;
		$min_size = 80;
		foreach($tag_array as $tag) {
			$tag_cloud[$tag['tag']] = $tag['count'];
		}
		$max_count = max(array_values($tag_cloud));
		$min_count = min(array_values($tag_cloud));
		$spread = $max_count - $min_count;
		if($spread == 0) {
			$spread = 1;
		}
		$step = ($max_size - $min_size) / $spread;
		foreach($tag_cloud as $key => $value) {
			$size = $min_size + (($value - $min_count) * $step);
			$size = ceil($size);
			$tag_list .= "<a class=\"tag\" href=\"/tags/" . $key . "\" title=\"" . preg_replace("/-/", " ", $key) . " (" . $value . ")\" style=\"font-size: " . $size . "%;\">" . $key . "</a> ";
		}
		
		$tag_list .= "</div>\n";
		
		return $tag_list;
	}

	return $tag_list;
}

function drizzo_tag_cloud($limit = 25) {
	$tag_cloud = drizzo_the_tag_list("cloud", $limit);
	return $tag_cloud;
}

function drizzo_post_permalink() {
	global $the_post, $drizzo_options, $the_posts;
	
	drizzo_db_connect();
	
	if(!$the_post) {
		return false;
	} else {
		$use_permalinks_query = mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='use_permalinks'");
		if(!$use_permalinks_query) {
			$use_permalinks = 0;
		} else {
			$use_permalinks = mysql_fetch_array($use_permalinks_query);
			$use_permalinks = $use_permalinks[0][value];

			if($use_permalinks == 0) {
				$the_permalink = "http://" . domain_name . "/?view=" . $the_post['ID'];
			} else {
				$the_permalink = "http://" . domain_name . "/" . $the_post['type'] . "/" . $the_post['permalink_name'] . "/";
			}
		}
		

		return $the_permalink;
	}
}

function drizzo_page_ancestor($id) {
	global $drizzo_posts;

	
	$patriarch_query = @mysql_query("SELECT `page_parent` FROM `$drizzo_posts` WHERE `ID`='$id'");
	if($patriarch_query) {
		while($row = @mysql_fetch_array($patriarch_query)) {
			$parent = $row['page_parent'];
			if($parent != 0) {
				drizzo_page_ancestor($parent);
			} else {
				return $id;
			}
		}
	} else {
		return false;
	}
}

function drizzo_page_list() {
	global $the_post, $drizzo_posts;

	$parent_id = drizzo_page_ancestor($the_post['ID']);
	$string = "<ul id=\"page_list\">\n";
	$sql = "SELECT `permalink_name`,`title` FROM `$drizzo_posts` WHERE `ID`='$patriarch_id'";
	$patriarch_name_query = @mysql_query($sql);
	if($patriarch_name_query) {
		while($row = mysql_fetch_array($patriarch_name_query)) {
			$patriarch_permalink = $row['permalink_name'];
			$string .= "<li><strong><a href=\"http://" . domain_name . "/" . $row['permalink_name'] . "\">" . $row['title'] . "</a></strong></li>";
		}
	}
	drizzo_page_list_loop($parent_id, 0, null, $patriarch_permalink);
	$string .= "</ul>\n";
	return $string;
}

function drizzo_page_list_loop($parent = 0, $sub_no = 0, $parent_permalink = null, $patriarch_permalink = null) {
	global $drizzo_posts, $the_post;

	$page_query = mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status`='publish' AND `page_parent`='$parent' AND `type`='page' ORDER BY `page_order`,`ID` ASC");
	
	if(!$the_post) {
		$parent_id = 0;
	} else {
		$parent_id = $the_post['page_parent'];
	}
	
	while($row = mysql_fetch_array($page_query)) {

		$id = "";
		$title = "";
		$page_parent = "";
		$permalink = "";
		$link = "";

		$id = $row['ID'];
		$title = $row['title'];
		$page_parent = $row['page_parent'];
		$permalink = $row['permalink_name'];
		$link = "http://" . domain_name . "/" . $patriarch_permalink . "/" . $parent_permalink . $permalink;

		if($page_parent == $parent) {

			echo "\t<li style=\"padding-left: " . ($sub_no + 1) . ".5em;\"><a href=\"" . $link . "\">" . $title . "</a></li>\n";

			drizzo_page_list_loop($id, $sub_no +1, $parent_permalink, $patriarch_permalink);
		}
	}
}

function drizzo_tag_search($tag = null, $limit = 0, $offset = 0) {
	global $drizzo_posts, $drizzo_comments, $drizzo_users;
	
	if($tag == null) {
		return false;
	} else {
		if($limit == 0) {
			$the_posts_query = mysql_query("SELECT * FROM `$drizzo_posts` WHERE `tags` LIKE '%$tag%' ORDER BY `ID` DESC");
		} else {
			$the_posts_query = mysql_query("SELECT * FROM `$drizzo_posts` WHERE `tags` LIKE '%$tag%' ORDER BY `ID` DESC LIMIT $offset,$limit");
		}

		if(!$the_posts_query) {
			echo "<p>Posts could not be retrieved.</p>";
		} else {
			while($row = mysql_fetch_array($the_posts_query)) {
				$author_query = mysql_query("SELECT `nice_name`,`username` FROM `$drizzo_users` WHERE `ID`='$row[author]'");
				if($author_query) {
					while($author_row = mysql_fetch_array($author_query)) {
						$author = $author_row['nice_name'];
						if($author == "") {
							$author = $author_row['username'];
						}
					}				
				}

				$comments_query = mysql_query("SELECT * FROM `$drizzo_comments` WHERE `post_id`='$row[ID]'");
				if($comments_query) {
					$comments = mysql_num_rows($comments_query);				
				} else {
					$comments = 0;
				}

				$the_posts[] = array(
					"ID" => $row['ID'],
					"author" => $row['author'],
					"author_nicename" => $author,
					"type" => $row['type'],
					"status" => $row['status'],
					"page_parent" => $row['page_parent'],
					"page_order" => $row['page_order'],
					"title" => stripslashes($row['title']),
					"content" => stripslashes($row['content']),
					"tags" => $row['tags'],
					"comment_status" => $row['comment_status'],
					"comment_count" => $comments,
					"posted_date" => (date("M d, Y", $row['posted_date']) . " at " . date("g:i A", $row['posted_date'])),
					"posted_date_raw" => $row['posted_date'],
					"edited_date" => (date("M d, Y", $row['edited_date']) . " at " . date("g:i A", $row['edited_date'])),
					"edited_date_raw" => $row['edited_date'],
					"edited_by" => $row['edited_by'],
					"menu_name" => $row['menu_name'],
					"permalink_name" => $row['permalink_name'],
					"lat" => $row['lat'],
					"lon" => $row['lon']
					);

			}
		}
		return $the_posts;
	}
	
}
?>